﻿using System;
using System.Collections.Generic;
using System.Globalization;
using Medianamik.Core;
using Medianamik.Core.Synchronization;

namespace Medianamik.UI.Web.Shop.Data.Repositories
{
    public interface IAddressRecordRepository
    {
        IEnumerable<Record> GetAddresses(Guid nodeId);
    }

    public abstract class AddressRecordRepository<TAddress> : IAddressRecordRepository
        where TAddress: Interfaces.IAddress
    {
        protected readonly IAddressRepository<TAddress> AddressRepository;

        public AddressRecordRepository(IAddressRepository<TAddress> addressRepository)
        {
            AddressRepository = addressRepository;
        }

        public abstract IEnumerable<Record> GetAddresses(Guid nodeId);
    }

    public class AddressRecordRepository : AddressRecordRepository<Address>
    {
        public AddressRecordRepository(IAddressRepository<Address> addressRepository) : base(addressRepository)
        {
        }

        public override IEnumerable<Record> GetAddresses(Guid nodeId)
        {
            var informationMappings = new List<PropertyMapping>
                  {
                      new PropertyMapping(Address.Columns.Name, Address.Columns.Name, CultureInfo.InvariantCulture.Name),
                      new PropertyMapping(Address.Columns.AddressOne, Address.Columns.AddressOne, CultureInfo.InvariantCulture.Name),
                      new PropertyMapping(Address.Columns.AddressTwo, Address.Columns.AddressTwo, CultureInfo.InvariantCulture.Name),
                      new PropertyMapping(Address.Columns.Country, Address.Columns.Country, CultureInfo.InvariantCulture.Name),
                      new PropertyMapping(Address.Columns.State, Address.Columns.State, CultureInfo.InvariantCulture.Name),
                      new PropertyMapping(Address.Columns.City, Address.Columns.City, CultureInfo.InvariantCulture.Name),
                      new PropertyMapping(Address.Columns.ZipCode, Address.Columns.ZipCode, CultureInfo.InvariantCulture.Name),
                      new PropertyMapping(Address.Columns.IsDefault, Address.Columns.IsDefault, CultureInfo.InvariantCulture.Name)
                  };

            var addresses = new AddressCollection();
            AddressRepository.ChildsOf(nodeId, false, 0,CultureInfo.InvariantCulture,
                ContentState.Draft,ActiveStatus.All).ForEach(addresses.Add);

            return new List<Record>(addresses.ToDataTable().ToMedianamikRecords(informationMappings,
                "Address", CultureInfo.InvariantCulture.Name, null));
        }
    }
}
